<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE Unsafe #-}</span><span>
</span><span id="line-2"></span><span class="hs-pragma">{-# LANGUAGE NoImplicitPrelude, MagicHash #-}</span><span>
</span><span id="line-3"></span><span class="hs-pragma">{-# LANGUAGE UnboxedTuples #-}</span><span>
</span><span id="line-4"></span><span class="hs-pragma">{-# LANGUAGE BangPatterns #-}</span><span>
</span><span id="line-5"></span><span class="hs-pragma">{-# OPTIONS_HADDOCK not-home #-}</span><span>
</span><span id="line-6"></span><span>
</span><span id="line-7"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-8"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-9"></span><span class="hs-comment">-- Module      :  GHC.IORef</span><span>
</span><span id="line-10"></span><span class="hs-comment">-- Copyright   :  (c) The University of Glasgow 2008</span><span>
</span><span id="line-11"></span><span class="hs-comment">-- License     :  see libraries/base/LICENSE</span><span>
</span><span id="line-12"></span><span class="hs-comment">--</span><span>
</span><span id="line-13"></span><span class="hs-comment">-- Maintainer  :  cvs-ghc@haskell.org</span><span>
</span><span id="line-14"></span><span class="hs-comment">-- Stability   :  internal</span><span>
</span><span id="line-15"></span><span class="hs-comment">-- Portability :  non-portable (GHC Extensions)</span><span>
</span><span id="line-16"></span><span class="hs-comment">--</span><span>
</span><span id="line-17"></span><span class="hs-comment">-- The IORef type</span><span>
</span><span id="line-18"></span><span class="hs-comment">--</span><span>
</span><span id="line-19"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-20"></span><span>
</span><span id="line-21"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">GHC.IORef</span><span> </span><span class="hs-special">(</span><span>
</span><span id="line-22"></span><span>        </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier">IORef</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span class="hs-special">,</span><span>
</span><span id="line-23"></span><span>        </span><span class="annot"><a href="GHC.IORef.html#newIORef"><span class="hs-identifier">newIORef</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.IORef.html#readIORef"><span class="hs-identifier">readIORef</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.IORef.html#writeIORef"><span class="hs-identifier">writeIORef</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.IORef.html#atomicModifyIORef2Lazy"><span class="hs-identifier">atomicModifyIORef2Lazy</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-24"></span><span>        </span><span class="annot"><a href="GHC.IORef.html#atomicModifyIORef2"><span class="hs-identifier">atomicModifyIORef2</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.IORef.html#atomicModifyIORefLazy_"><span class="hs-identifier">atomicModifyIORefLazy_</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.IORef.html#atomicModifyIORef%27_"><span class="hs-identifier">atomicModifyIORef'_</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-25"></span><span>        </span><span class="annot"><a href="GHC.IORef.html#atomicModifyIORefP"><span class="hs-identifier">atomicModifyIORefP</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.IORef.html#atomicSwapIORef"><span class="hs-identifier">atomicSwapIORef</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.IORef.html#atomicModifyIORef%27"><span class="hs-identifier">atomicModifyIORef'</span></a></span><span>
</span><span id="line-26"></span><span>    </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-27"></span><span>
</span><span id="line-28"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Base.html"><span class="hs-identifier">GHC.Base</span></a></span><span>
</span><span id="line-29"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.STRef.html"><span class="hs-identifier">GHC.STRef</span></a></span><span>
</span><span id="line-30"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.IO.html"><span class="hs-identifier">GHC.IO</span></a></span><span>
</span><span id="line-31"></span><span>
</span><span id="line-32"></span><span class="hs-comment">-- ---------------------------------------------------------------------------</span><span>
</span><span id="line-33"></span><span class="hs-comment">-- IORefs</span><span>
</span><span id="line-34"></span><span>
</span><span id="line-35"></span><span class="hs-comment">-- |A mutable variable in the 'IO' monad</span><span>
</span><span id="line-36"></span><span class="hs-keyword">newtype</span><span> </span><span id="IORef"><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-var">IORef</span></a></span></span><span> </span><span id="local-6989586621679531143"><span class="annot"><a href="#local-6989586621679531143"><span class="hs-identifier hs-type">a</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="IORef"><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-var">IORef</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.STRef.html#STRef"><span class="hs-identifier hs-type">STRef</span></a></span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#RealWorld"><span class="hs-identifier hs-type">RealWorld</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679531143"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-37"></span><span>  </span><span class="hs-keyword">deriving</span><span> </span><span id="local-6989586621679531079"><span id="local-6989586621679531084"><span class="annot"><span class="annottext">IORef a -&gt; IORef a -&gt; Bool
(IORef a -&gt; IORef a -&gt; Bool)
-&gt; (IORef a -&gt; IORef a -&gt; Bool) -&gt; Eq (IORef a)
forall a. IORef a -&gt; IORef a -&gt; Bool
forall a. (a -&gt; a -&gt; Bool) -&gt; (a -&gt; a -&gt; Bool) -&gt; Eq a
/= :: IORef a -&gt; IORef a -&gt; Bool
$c/= :: forall a. IORef a -&gt; IORef a -&gt; Bool
== :: IORef a -&gt; IORef a -&gt; Bool
$c== :: forall a. IORef a -&gt; IORef a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#Eq"><span class="hs-identifier hs-var hs-var hs-var hs-var hs-var hs-var">Eq</span></a></span></span></span><span>
</span><span id="line-38"></span><span>  </span><span class="hs-comment">-- ^ Pointer equality.</span><span>
</span><span id="line-39"></span><span>  </span><span class="hs-comment">--</span><span>
</span><span id="line-40"></span><span>  </span><span class="hs-comment">-- @since 4.0.0.0</span><span>
</span><span id="line-41"></span><span>
</span><span id="line-42"></span><span class="hs-comment">-- |Build a new 'IORef'</span><span>
</span><span id="line-43"></span><span id="local-6989586621679531142"><span class="annot"><a href="GHC.IORef.html#newIORef"><span class="hs-identifier hs-type">newIORef</span></a></span><span>    </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="#local-6989586621679531142"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679531142"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-44"></span><span id="newIORef"><span class="annot"><span class="annottext">newIORef :: forall a. a -&gt; IO (IORef a)
</span><a href="GHC.IORef.html#newIORef"><span class="hs-identifier hs-var hs-var">newIORef</span></a></span></span><span> </span><span id="local-6989586621679531071"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531071"><span class="hs-identifier hs-var">v</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">ST RealWorld (STRef RealWorld a) -&gt; IO (STRef RealWorld a)
forall a. ST RealWorld a -&gt; IO a
</span><a href="GHC.IO.html#stToIO"><span class="hs-identifier hs-var">stToIO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">a -&gt; ST RealWorld (STRef RealWorld a)
forall a s. a -&gt; ST s (STRef s a)
</span><a href="GHC.STRef.html#newSTRef"><span class="hs-identifier hs-var">newSTRef</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531071"><span class="hs-identifier hs-var">v</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">IO (STRef RealWorld a)
-&gt; (STRef RealWorld a -&gt; IO (IORef a)) -&gt; IO (IORef a)
forall (m :: * -&gt; *) a b. Monad m =&gt; m a -&gt; (a -&gt; m b) -&gt; m b
</span><a href="GHC.Base.html#%3E%3E%3D"><span class="hs-operator hs-var">&gt;&gt;=</span></a></span><span> </span><span class="hs-glyph">\</span><span> </span><span id="local-6989586621679531068"><span class="annot"><span class="annottext">STRef RealWorld a
</span><a href="#local-6989586621679531068"><span class="hs-identifier hs-var">var</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">IORef a -&gt; IO (IORef a)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">STRef RealWorld a -&gt; IORef a
forall a. STRef RealWorld a -&gt; IORef a
</span><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-var">IORef</span></a></span><span> </span><span class="annot"><span class="annottext">STRef RealWorld a
</span><a href="#local-6989586621679531068"><span class="hs-identifier hs-var">var</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-45"></span><span>
</span><span id="line-46"></span><span class="hs-comment">-- |Read the value of an 'IORef'</span><span>
</span><span id="line-47"></span><span id="local-6989586621679531132"><span class="annot"><a href="GHC.IORef.html#readIORef"><span class="hs-identifier hs-type">readIORef</span></a></span><span>   </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679531132"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679531132"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-48"></span><span id="readIORef"><span class="annot"><span class="annottext">readIORef :: forall a. IORef a -&gt; IO a
</span><a href="GHC.IORef.html#readIORef"><span class="hs-identifier hs-var hs-var">readIORef</span></a></span></span><span>  </span><span class="hs-special">(</span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span id="local-6989586621679531067"><span class="annot"><span class="annottext">STRef RealWorld a
</span><a href="#local-6989586621679531067"><span class="hs-identifier hs-var">var</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">ST RealWorld a -&gt; IO a
forall a. ST RealWorld a -&gt; IO a
</span><a href="GHC.IO.html#stToIO"><span class="hs-identifier hs-var">stToIO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">STRef RealWorld a -&gt; ST RealWorld a
forall s a. STRef s a -&gt; ST s a
</span><a href="GHC.STRef.html#readSTRef"><span class="hs-identifier hs-var">readSTRef</span></a></span><span> </span><span class="annot"><span class="annottext">STRef RealWorld a
</span><a href="#local-6989586621679531067"><span class="hs-identifier hs-var">var</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-49"></span><span>
</span><span id="line-50"></span><span class="hs-comment">-- |Write a new value into an 'IORef'</span><span>
</span><span id="line-51"></span><span id="local-6989586621679531128"><span class="annot"><a href="GHC.IORef.html#writeIORef"><span class="hs-identifier hs-type">writeIORef</span></a></span><span>  </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679531128"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679531128"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-52"></span><span id="writeIORef"><span class="annot"><span class="annottext">writeIORef :: forall a. IORef a -&gt; a -&gt; IO ()
</span><a href="GHC.IORef.html#writeIORef"><span class="hs-identifier hs-var hs-var">writeIORef</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span id="local-6989586621679531065"><span class="annot"><span class="annottext">STRef RealWorld a
</span><a href="#local-6989586621679531065"><span class="hs-identifier hs-var">var</span></a></span></span><span class="hs-special">)</span><span> </span><span id="local-6989586621679531064"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531064"><span class="hs-identifier hs-var">v</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">ST RealWorld () -&gt; IO ()
forall a. ST RealWorld a -&gt; IO a
</span><a href="GHC.IO.html#stToIO"><span class="hs-identifier hs-var">stToIO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">STRef RealWorld a -&gt; a -&gt; ST RealWorld ()
forall s a. STRef s a -&gt; a -&gt; ST s ()
</span><a href="GHC.STRef.html#writeSTRef"><span class="hs-identifier hs-var">writeSTRef</span></a></span><span> </span><span class="annot"><span class="annottext">STRef RealWorld a
</span><a href="#local-6989586621679531065"><span class="hs-identifier hs-var">var</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531064"><span class="hs-identifier hs-var">v</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-53"></span><span>
</span><span id="line-54"></span><span class="hs-comment">-- Atomically apply a function to the contents of an 'IORef',</span><span>
</span><span id="line-55"></span><span class="hs-comment">-- installing its first component in the 'IORef' and returning</span><span>
</span><span id="line-56"></span><span class="hs-comment">-- the old contents and the result of applying the function.</span><span>
</span><span id="line-57"></span><span class="hs-comment">-- The result of the function application (the pair) is not forced.</span><span>
</span><span id="line-58"></span><span class="hs-comment">-- As a result, this can lead to memory leaks. It is generally better</span><span>
</span><span id="line-59"></span><span class="hs-comment">-- to use 'atomicModifyIORef2'.</span><span>
</span><span id="line-60"></span><span id="local-6989586621679531123"><span id="local-6989586621679531124"><span class="annot"><a href="GHC.IORef.html#atomicModifyIORef2Lazy"><span class="hs-identifier hs-type">atomicModifyIORef2Lazy</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679531124"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531124"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531124"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">,</span><span class="annot"><a href="#local-6989586621679531123"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531124"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531124"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="#local-6989586621679531123"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span></span></span><span>
</span><span id="line-61"></span><span id="atomicModifyIORef2Lazy"><span class="annot"><span class="annottext">atomicModifyIORef2Lazy :: forall a b. IORef a -&gt; (a -&gt; (a, b)) -&gt; IO (a, (a, b))
</span><a href="GHC.IORef.html#atomicModifyIORef2Lazy"><span class="hs-identifier hs-var hs-var">atomicModifyIORef2Lazy</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.STRef.html#STRef"><span class="hs-identifier hs-type">STRef</span></a></span><span> </span><span id="local-6989586621679531061"><span class="annot"><span class="annottext">MutVar# RealWorld a
</span><a href="#local-6989586621679531061"><span class="hs-identifier hs-var">r#</span></a></span></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span id="local-6989586621679531060"><span class="annot"><span class="annottext">a -&gt; (a, b)
</span><a href="#local-6989586621679531060"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-62"></span><span>  </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, (a, (a, b)) #))
-&gt; IO (a, (a, b))
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span id="local-6989586621679531059"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679531059"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">MutVar# RealWorld a
-&gt; (a -&gt; (a, b))
-&gt; State# RealWorld
-&gt; (# State# RealWorld, a, (a, b) #)
forall d a c.
MutVar# d a -&gt; (a -&gt; c) -&gt; State# d -&gt; (# State# d, a, c #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#atomicModifyMutVar2%23"><span class="hs-identifier hs-var">atomicModifyMutVar2#</span></a></span><span> </span><span class="annot"><span class="annottext">MutVar# RealWorld a
</span><a href="#local-6989586621679531061"><span class="hs-identifier hs-var">r#</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; (a, b)
</span><a href="#local-6989586621679531060"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679531059"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-63"></span><span>              </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679531058"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679531058"><span class="hs-identifier hs-var">s'</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679531057"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531057"><span class="hs-identifier hs-var">old</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679531056"><span class="annot"><span class="annottext">(a, b)
</span><a href="#local-6989586621679531056"><span class="hs-identifier hs-var">res</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679531058"><span class="hs-identifier hs-var">s'</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531057"><span class="hs-identifier hs-var">old</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">(a, b)
</span><a href="#local-6989586621679531056"><span class="hs-identifier hs-var">res</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span class="hs-special">)</span><span>
</span><span id="line-64"></span><span>
</span><span id="line-65"></span><span class="hs-comment">-- Atomically apply a function to the contents of an 'IORef',</span><span>
</span><span id="line-66"></span><span class="hs-comment">-- installing its first component in the 'IORef' and returning</span><span>
</span><span id="line-67"></span><span class="hs-comment">-- the old contents and the result of applying the function.</span><span>
</span><span id="line-68"></span><span class="hs-comment">-- The result of the function application (the pair) is forced,</span><span>
</span><span id="line-69"></span><span class="hs-comment">-- but neither of its components is.</span><span>
</span><span id="line-70"></span><span id="local-6989586621679531054"><span id="local-6989586621679531055"><span class="annot"><a href="GHC.IORef.html#atomicModifyIORef2"><span class="hs-identifier hs-type">atomicModifyIORef2</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679531055"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531055"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531055"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">,</span><span class="annot"><a href="#local-6989586621679531054"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531055"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531055"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="#local-6989586621679531054"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span></span></span><span>
</span><span id="line-71"></span><span id="atomicModifyIORef2"><span class="annot"><span class="annottext">atomicModifyIORef2 :: forall a b. IORef a -&gt; (a -&gt; (a, b)) -&gt; IO (a, (a, b))
</span><a href="GHC.IORef.html#atomicModifyIORef2"><span class="hs-identifier hs-var hs-var">atomicModifyIORef2</span></a></span></span><span> </span><span id="local-6989586621679531051"><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679531051"><span class="hs-identifier hs-var">ref</span></a></span></span><span> </span><span id="local-6989586621679531050"><span class="annot"><span class="annottext">a -&gt; (a, b)
</span><a href="#local-6989586621679531050"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-72"></span><span>  </span><span id="local-6989586621679531049"><span class="annot"><span class="annottext">r :: (a, (a, b))
</span><a href="#local-6989586621679531049"><span class="hs-identifier hs-var">r</span></a></span></span><span class="hs-glyph">@</span><span class="hs-special">(</span><span id="local-6989586621679531048"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531048"><span class="hs-identifier hs-var">_old</span></a></span></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span id="local-6989586621679531047"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531047"><span class="hs-identifier hs-var">_new</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679531046"><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679531046"><span class="hs-identifier hs-var">_res</span></a></span></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IORef a -&gt; (a -&gt; (a, b)) -&gt; IO (a, (a, b))
forall a b. IORef a -&gt; (a -&gt; (a, b)) -&gt; IO (a, (a, b))
</span><a href="GHC.IORef.html#atomicModifyIORef2Lazy"><span class="hs-identifier hs-var">atomicModifyIORef2Lazy</span></a></span><span> </span><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679531051"><span class="hs-identifier hs-var">ref</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; (a, b)
</span><a href="#local-6989586621679531050"><span class="hs-identifier hs-var">f</span></a></span><span>
</span><span id="line-73"></span><span>  </span><span class="annot"><span class="annottext">(a, (a, b)) -&gt; IO (a, (a, b))
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="annot"><span class="annottext">(a, (a, b))
</span><a href="#local-6989586621679531049"><span class="hs-identifier hs-var">r</span></a></span><span>
</span><span id="line-74"></span><span>
</span><span id="line-75"></span><span class="hs-comment">-- | A version of 'Data.IORef.atomicModifyIORef' that forces</span><span>
</span><span id="line-76"></span><span class="hs-comment">-- the (pair) result of the function.</span><span>
</span><span id="line-77"></span><span id="local-6989586621679531113"><span id="local-6989586621679531114"><span class="annot"><a href="GHC.IORef.html#atomicModifyIORefP"><span class="hs-identifier hs-type">atomicModifyIORefP</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679531114"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531114"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531114"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">,</span><span class="annot"><a href="#local-6989586621679531113"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679531113"><span class="hs-identifier hs-type">b</span></a></span></span></span><span>
</span><span id="line-78"></span><span id="atomicModifyIORefP"><span class="annot"><span class="annottext">atomicModifyIORefP :: forall a b. IORef a -&gt; (a -&gt; (a, b)) -&gt; IO b
</span><a href="GHC.IORef.html#atomicModifyIORefP"><span class="hs-identifier hs-var hs-var">atomicModifyIORefP</span></a></span></span><span> </span><span id="local-6989586621679531042"><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679531042"><span class="hs-identifier hs-var">ref</span></a></span></span><span> </span><span id="local-6989586621679531041"><span class="annot"><span class="annottext">a -&gt; (a, b)
</span><a href="#local-6989586621679531041"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-79"></span><span>  </span><span class="hs-special">(</span><span id="local-6989586621679531040"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531040"><span class="hs-identifier hs-var">_old</span></a></span></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">a
</span><span class="hs-identifier">_</span></span><span class="hs-special">,</span><span id="local-6989586621679531039"><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679531039"><span class="hs-identifier hs-var">r</span></a></span></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IORef a -&gt; (a -&gt; (a, b)) -&gt; IO (a, (a, b))
forall a b. IORef a -&gt; (a -&gt; (a, b)) -&gt; IO (a, (a, b))
</span><a href="GHC.IORef.html#atomicModifyIORef2"><span class="hs-identifier hs-var">atomicModifyIORef2</span></a></span><span> </span><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679531042"><span class="hs-identifier hs-var">ref</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; (a, b)
</span><a href="#local-6989586621679531041"><span class="hs-identifier hs-var">f</span></a></span><span>
</span><span id="line-80"></span><span>  </span><span class="annot"><span class="annottext">b -&gt; IO b
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679531039"><span class="hs-identifier hs-var">r</span></a></span><span>
</span><span id="line-81"></span><span>
</span><span id="line-82"></span><span class="hs-comment">-- | Atomically apply a function to the contents of an</span><span>
</span><span id="line-83"></span><span class="hs-comment">-- 'IORef' and return the old and new values. The result</span><span>
</span><span id="line-84"></span><span class="hs-comment">-- of the function is not forced. As this can lead to a</span><span>
</span><span id="line-85"></span><span class="hs-comment">-- memory leak, it is usually better to use `atomicModifyIORef'_`.</span><span>
</span><span id="line-86"></span><span id="local-6989586621679531108"><span class="annot"><a href="GHC.IORef.html#atomicModifyIORefLazy_"><span class="hs-identifier hs-type">atomicModifyIORefLazy_</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679531108"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531108"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679531108"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531108"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="#local-6989586621679531108"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-87"></span><span id="atomicModifyIORefLazy_"><span class="annot"><span class="annottext">atomicModifyIORefLazy_ :: forall a. IORef a -&gt; (a -&gt; a) -&gt; IO (a, a)
</span><a href="GHC.IORef.html#atomicModifyIORefLazy_"><span class="hs-identifier hs-var hs-var">atomicModifyIORefLazy_</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.STRef.html#STRef"><span class="hs-identifier hs-type">STRef</span></a></span><span> </span><span id="local-6989586621679531038"><span class="annot"><span class="annottext">MutVar# RealWorld a
</span><a href="#local-6989586621679531038"><span class="hs-identifier hs-var">ref</span></a></span></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span id="local-6989586621679531037"><span class="annot"><span class="annottext">a -&gt; a
</span><a href="#local-6989586621679531037"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, (a, a) #)) -&gt; IO (a, a)
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, (a, a) #)) -&gt; IO (a, a))
-&gt; (State# RealWorld -&gt; (# State# RealWorld, (a, a) #))
-&gt; IO (a, a)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679531036"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679531036"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-88"></span><span>  </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">MutVar# RealWorld a
-&gt; (a -&gt; a) -&gt; State# RealWorld -&gt; (# State# RealWorld, a, a #)
forall d a.
MutVar# d a -&gt; (a -&gt; a) -&gt; State# d -&gt; (# State# d, a, a #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#atomicModifyMutVar_%23"><span class="hs-identifier hs-var">atomicModifyMutVar_#</span></a></span><span> </span><span class="annot"><span class="annottext">MutVar# RealWorld a
</span><a href="#local-6989586621679531038"><span class="hs-identifier hs-var">ref</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; a
</span><a href="#local-6989586621679531037"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679531036"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-89"></span><span>    </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679531035"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679531035"><span class="hs-identifier hs-var">s'</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679531034"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531034"><span class="hs-identifier hs-var">old</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679531033"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531033"><span class="hs-identifier hs-var">new</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679531035"><span class="hs-identifier hs-var">s'</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531034"><span class="hs-identifier hs-var">old</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531033"><span class="hs-identifier hs-var">new</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-90"></span><span>
</span><span id="line-91"></span><span class="hs-comment">-- | Atomically apply a function to the contents of an</span><span>
</span><span id="line-92"></span><span class="hs-comment">-- 'IORef' and return the old and new values. The result</span><span>
</span><span id="line-93"></span><span class="hs-comment">-- of the function is forced.</span><span>
</span><span id="line-94"></span><span id="local-6989586621679531032"><span class="annot"><a href="GHC.IORef.html#atomicModifyIORef%27_"><span class="hs-identifier hs-type">atomicModifyIORef'_</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679531032"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531032"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679531032"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531032"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="#local-6989586621679531032"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-95"></span><span id="atomicModifyIORef%27_"><span class="annot"><span class="annottext">atomicModifyIORef'_ :: forall a. IORef a -&gt; (a -&gt; a) -&gt; IO (a, a)
</span><a href="GHC.IORef.html#atomicModifyIORef%27_"><span class="hs-identifier hs-var hs-var">atomicModifyIORef'_</span></a></span></span><span> </span><span id="local-6989586621679531029"><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679531029"><span class="hs-identifier hs-var">ref</span></a></span></span><span> </span><span id="local-6989586621679531028"><span class="annot"><span class="annottext">a -&gt; a
</span><a href="#local-6989586621679531028"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-96"></span><span>  </span><span class="hs-special">(</span><span id="local-6989586621679531027"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531027"><span class="hs-identifier hs-var">old</span></a></span></span><span class="hs-special">,</span><span> </span><span class="hs-glyph">!</span><span id="local-6989586621679531026"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531026"><span class="hs-identifier hs-var">new</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IORef a -&gt; (a -&gt; a) -&gt; IO (a, a)
forall a. IORef a -&gt; (a -&gt; a) -&gt; IO (a, a)
</span><a href="GHC.IORef.html#atomicModifyIORefLazy_"><span class="hs-identifier hs-var">atomicModifyIORefLazy_</span></a></span><span> </span><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679531029"><span class="hs-identifier hs-var">ref</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; a
</span><a href="#local-6989586621679531028"><span class="hs-identifier hs-var">f</span></a></span><span>
</span><span id="line-97"></span><span>  </span><span class="annot"><span class="annottext">(a, a) -&gt; IO (a, a)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531027"><span class="hs-identifier hs-var">old</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531026"><span class="hs-identifier hs-var">new</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-98"></span><span>
</span><span id="line-99"></span><span class="hs-comment">-- | Atomically replace the contents of an 'IORef', returning</span><span>
</span><span id="line-100"></span><span class="hs-comment">-- the old contents.</span><span>
</span><span id="line-101"></span><span id="local-6989586621679531102"><span class="annot"><a href="GHC.IORef.html#atomicSwapIORef"><span class="hs-identifier hs-type">atomicSwapIORef</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679531102"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679531102"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679531102"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-102"></span><span class="hs-comment">-- Bad implementation! This will be a primop shortly.</span><span>
</span><span id="line-103"></span><span id="atomicSwapIORef"><span class="annot"><span class="annottext">atomicSwapIORef :: forall a. IORef a -&gt; a -&gt; IO a
</span><a href="GHC.IORef.html#atomicSwapIORef"><span class="hs-identifier hs-var hs-var">atomicSwapIORef</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.STRef.html#STRef"><span class="hs-identifier hs-type">STRef</span></a></span><span> </span><span id="local-6989586621679531025"><span class="annot"><span class="annottext">MutVar# RealWorld a
</span><a href="#local-6989586621679531025"><span class="hs-identifier hs-var">ref</span></a></span></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span id="local-6989586621679531024"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531024"><span class="hs-identifier hs-var">new</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a)
-&gt; (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679531023"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679531023"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-104"></span><span>  </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">MutVar# RealWorld a
-&gt; (a -&gt; Box a)
-&gt; State# RealWorld
-&gt; (# State# RealWorld, a, Box a #)
forall d a c.
MutVar# d a -&gt; (a -&gt; c) -&gt; State# d -&gt; (# State# d, a, c #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#atomicModifyMutVar2%23"><span class="hs-identifier hs-var">atomicModifyMutVar2#</span></a></span><span> </span><span class="annot"><span class="annottext">MutVar# RealWorld a
</span><a href="#local-6989586621679531025"><span class="hs-identifier hs-var">ref</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span id="local-6989586621679531022"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531022"><span class="hs-identifier hs-var">_old</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">a -&gt; Box a
forall a. a -&gt; Box a
</span><a href="GHC.IORef.html#Box"><span class="hs-identifier hs-var">Box</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531024"><span class="hs-identifier hs-var">new</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679531023"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-105"></span><span>    </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679531020"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679531020"><span class="hs-identifier hs-var">s'</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679531019"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531019"><span class="hs-identifier hs-var">old</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.IORef.html#Box"><span class="hs-identifier hs-type">Box</span></a></span><span> </span><span id="local-6989586621679531018"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531018"><span class="hs-identifier hs-var">_new</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679531020"><span class="hs-identifier hs-var">s'</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531019"><span class="hs-identifier hs-var">old</span></a></span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-106"></span><span>
</span><span id="line-107"></span><span class="hs-keyword">data</span><span> </span><span id="Box"><span class="annot"><a href="GHC.IORef.html#Box"><span class="hs-identifier hs-var">Box</span></a></span></span><span> </span><span id="local-6989586621679531099"><span class="annot"><a href="#local-6989586621679531099"><span class="hs-identifier hs-type">a</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="Box"><span class="annot"><a href="GHC.IORef.html#Box"><span class="hs-identifier hs-var">Box</span></a></span></span><span> </span><span class="annot"><a href="#local-6989586621679531099"><span class="hs-identifier hs-type">a</span></a></span><span>
</span><span id="line-108"></span><span>
</span><span id="line-109"></span><span class="hs-comment">-- | Strict version of 'Data.IORef.atomicModifyIORef'. This forces both</span><span>
</span><span id="line-110"></span><span class="hs-comment">-- the value stored in the 'IORef' and the value returned. The new value</span><span>
</span><span id="line-111"></span><span class="hs-comment">-- is installed in the 'IORef' before the returned value is forced.</span><span>
</span><span id="line-112"></span><span class="hs-comment">-- So</span><span>
</span><span id="line-113"></span><span class="hs-comment">--</span><span>
</span><span id="line-114"></span><span class="hs-comment">-- @atomicModifyIORef' ref (\x -&gt; (x+1, undefined))@</span><span>
</span><span id="line-115"></span><span class="hs-comment">--</span><span>
</span><span id="line-116"></span><span class="hs-comment">-- will increment the 'IORef' and then throw an exception in the calling</span><span>
</span><span id="line-117"></span><span class="hs-comment">-- thread.</span><span>
</span><span id="line-118"></span><span class="hs-comment">--</span><span>
</span><span id="line-119"></span><span class="hs-comment">-- @since 4.6.0.0</span><span>
</span><span id="line-120"></span><span id="local-6989586621679531016"><span id="local-6989586621679531017"><span class="annot"><a href="GHC.IORef.html#atomicModifyIORef%27"><span class="hs-identifier hs-type">atomicModifyIORef'</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679531017"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531017"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679531017"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">,</span><span class="annot"><a href="#local-6989586621679531016"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679531016"><span class="hs-identifier hs-type">b</span></a></span></span></span><span>
</span><span id="line-121"></span><span class="hs-comment">-- See Note [atomicModifyIORef' definition]</span><span>
</span><span id="line-122"></span><span id="atomicModifyIORef%27"><span class="annot"><span class="annottext">atomicModifyIORef' :: forall a b. IORef a -&gt; (a -&gt; (a, b)) -&gt; IO b
</span><a href="GHC.IORef.html#atomicModifyIORef%27"><span class="hs-identifier hs-var hs-var">atomicModifyIORef'</span></a></span></span><span> </span><span id="local-6989586621679531013"><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679531013"><span class="hs-identifier hs-var">ref</span></a></span></span><span> </span><span id="local-6989586621679531012"><span class="annot"><span class="annottext">a -&gt; (a, b)
</span><a href="#local-6989586621679531012"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-123"></span><span>  </span><span class="hs-special">(</span><span id="local-6989586621679531011"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531011"><span class="hs-identifier hs-var">_old</span></a></span></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span id="local-6989586621679531010"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531010"><span class="hs-identifier hs-var">_new</span></a></span></span><span class="hs-special">,</span><span> </span><span class="hs-glyph">!</span><span id="local-6989586621679531009"><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679531009"><span class="hs-identifier hs-var">res</span></a></span></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IORef a -&gt; (a -&gt; (a, b)) -&gt; IO (a, (a, b))
forall a b. IORef a -&gt; (a -&gt; (a, b)) -&gt; IO (a, (a, b))
</span><a href="GHC.IORef.html#atomicModifyIORef2"><span class="hs-identifier hs-var">atomicModifyIORef2</span></a></span><span> </span><span class="annot"><span class="annottext">IORef a
</span><a href="#local-6989586621679531013"><span class="hs-identifier hs-var">ref</span></a></span><span> </span><span class="annot"><span class="annottext">((a -&gt; (a, b)) -&gt; IO (a, (a, b)))
-&gt; (a -&gt; (a, b)) -&gt; IO (a, (a, b))
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-124"></span><span>    </span><span class="hs-glyph">\</span><span id="local-6989586621679531008"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531008"><span class="hs-identifier hs-var">old</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">a -&gt; (a, b)
</span><a href="#local-6989586621679531012"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531008"><span class="hs-identifier hs-var">old</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-125"></span><span>       </span><span id="local-6989586621679531007"><span class="annot"><span class="annottext">r :: (a, b)
</span><a href="#local-6989586621679531007"><span class="hs-identifier hs-var">r</span></a></span></span><span class="hs-glyph">@</span><span class="hs-special">(</span><span class="hs-glyph">!</span><span id="local-6989586621679531006"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679531006"><span class="hs-identifier hs-var">_new</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679531005"><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679531005"><span class="hs-identifier hs-var">_res</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">(a, b)
</span><a href="#local-6989586621679531007"><span class="hs-identifier hs-var">r</span></a></span><span>
</span><span id="line-126"></span><span>  </span><span class="annot"><span class="annottext">b -&gt; IO b
forall (f :: * -&gt; *) a. Applicative f =&gt; a -&gt; f a
</span><a href="GHC.Base.html#pure"><span class="hs-identifier hs-var">pure</span></a></span><span> </span><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679531009"><span class="hs-identifier hs-var">res</span></a></span><span>
</span><span id="line-127"></span><span>
</span><span id="line-128"></span><span class="hs-comment">-- Note [atomicModifyIORef' definition]</span><span>
</span><span id="line-129"></span><span class="hs-comment">-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span><span>
</span><span id="line-130"></span><span class="hs-comment">--</span><span>
</span><span id="line-131"></span><span class="hs-comment">-- atomicModifyIORef' was historically defined</span><span>
</span><span id="line-132"></span><span class="hs-comment">--</span><span>
</span><span id="line-133"></span><span class="hs-comment">--    atomicModifyIORef' ref f = do</span><span>
</span><span id="line-134"></span><span class="hs-comment">--        b &lt;- atomicModifyIORef ref $ \a -&gt;</span><span>
</span><span id="line-135"></span><span class="hs-comment">--                case f a of</span><span>
</span><span id="line-136"></span><span class="hs-comment">--                    v@(a',_) -&gt; a' `seq` v</span><span>
</span><span id="line-137"></span><span class="hs-comment">--        b `seq` return b</span><span>
</span><span id="line-138"></span><span class="hs-comment">--</span><span>
</span><span id="line-139"></span><span class="hs-comment">-- The most obvious definition, now that we have atomicModifyMutVar2#,</span><span>
</span><span id="line-140"></span><span class="hs-comment">-- would be</span><span>
</span><span id="line-141"></span><span class="hs-comment">--</span><span>
</span><span id="line-142"></span><span class="hs-comment">--    atomicModifyIORef' ref f = do</span><span>
</span><span id="line-143"></span><span class="hs-comment">--      (_old, (!_new, !res)) &lt;- atomicModifyIORef2 ref f</span><span>
</span><span id="line-144"></span><span class="hs-comment">--      pure res</span><span>
</span><span id="line-145"></span><span class="hs-comment">--</span><span>
</span><span id="line-146"></span><span class="hs-comment">-- Why do we force the new value on the &quot;inside&quot; instead of afterwards?</span><span>
</span><span id="line-147"></span><span class="hs-comment">-- I initially thought the latter would be okay, but then I realized</span><span>
</span><span id="line-148"></span><span class="hs-comment">-- that if we write</span><span>
</span><span id="line-149"></span><span class="hs-comment">--</span><span>
</span><span id="line-150"></span><span class="hs-comment">--   atomicModifyIORef' ref $ \x -&gt; (x + 5, x - 5)</span><span>
</span><span id="line-151"></span><span class="hs-comment">--</span><span>
</span><span id="line-152"></span><span class="hs-comment">-- then we'll end up building a pair of thunks to calculate x + 5</span><span>
</span><span id="line-153"></span><span class="hs-comment">-- and x - 5. That's no good! With the more complicated definition,</span><span>
</span><span id="line-154"></span><span class="hs-comment">-- we avoid this problem; the result pair is strict in the new IORef</span><span>
</span><span id="line-155"></span><span class="hs-comment">-- contents. Of course, if the function passed to atomicModifyIORef'</span><span>
</span><span id="line-156"></span><span class="hs-comment">-- doesn't inline, we'll build a closure for it. But that was already</span><span>
</span><span id="line-157"></span><span class="hs-comment">-- true for the historical definition of atomicModifyIORef' (in terms</span><span>
</span><span id="line-158"></span><span class="hs-comment">-- of atomicModifyIORef), so we shouldn't lose anything. Note that</span><span>
</span><span id="line-159"></span><span class="hs-comment">-- in keeping with the historical behavior, we *don't* propagate the</span><span>
</span><span id="line-160"></span><span class="hs-comment">-- strict demand on the result inwards. In particular,</span><span>
</span><span id="line-161"></span><span class="hs-comment">--</span><span>
</span><span id="line-162"></span><span class="hs-comment">--   atomicModifyIORef' ref (\x -&gt; (x + 1, undefined))</span><span>
</span><span id="line-163"></span><span class="hs-comment">--</span><span>
</span><span id="line-164"></span><span class="hs-comment">-- will increment the IORef and throw an exception; it will not</span><span>
</span><span id="line-165"></span><span class="hs-comment">-- install an undefined value in the IORef.</span><span>
</span><span id="line-166"></span><span class="hs-comment">--</span><span>
</span><span id="line-167"></span><span class="hs-comment">-- A clearer version, in my opinion (but one quite incompatible with</span><span>
</span><span id="line-168"></span><span class="hs-comment">-- the traditional one) would only force the new IORef value and not</span><span>
</span><span id="line-169"></span><span class="hs-comment">-- the result. This version would have been relatively inefficient</span><span>
</span><span id="line-170"></span><span class="hs-comment">-- to implement using atomicModifyMutVar#, but is just fine now.</span><span>
</span><span id="line-171"></span></pre></body></html>